import actions from '../constant'
import http from '../utils/http'

export function signin(object) {
  return {
    type: actions.SIGNIN,
    user: object
  }
}

export function before_fetch() {
  return {
    type: actions.BEFORE_FETCH
  }
}

export function fail_fetch(err) {
  return {
    type: actions.FAIL_FETCH,
    error: err
  }
}

export function fetchData(data) {
  return async dispatch => {
    dispatch(before_fetch())
    const res = await http.post('/api/signin', data).catch(err => {dispatch(fail_fetch(err))})
    sessionStorage.setItem('token', res.data.token)
    dispatch(signin(res))
    return res
  }
}